package com.zxymq.consumer.receiver;

import com.zxymq.amqp.listener.AbstractMessageListener;
import com.zxymq.constants.MQBusinessConstants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.stereotype.Component;

/**
 * 用户信息消费，一个例子
 * Created by Administrator on 2018/1/10.
 */
@Component
public class UsermodelMessageListenter extends AbstractMessageListener {
    //测试集合
  /*  @RabbitHandler
    @RabbitListener(queues = MQBusinessConstants.BUS_QUEUE)
    @Override
    public void receiveMessage(Message message, MessageConverter messageConverter) {
       List<Order> userInfos = (List<Order>) messageConverter.fromMessage(message);
        for (Order userInfo : userInfos) {
            System.out.println("消费者收到消息:" + userInfo.toString());
        }
    }*/
    //测试map
/*    @RabbitHandler
    @RabbitListener(queues = MQBusinessConstants.BUS_QUEUE)
    @Override
    public void receiveMessage(Message message, MessageConverter messageConverter) {
        Map<String, Order> userInfos = (Map<String, Order>) messageConverter.fromMessage(message);
        for (Map.Entry<String, Order> entry : userInfos.entrySet()) {
            String id = entry.getKey();
            Order order = entry.getValue();
            System.out.println("消费者收到消息:" + order.toString());
        }
    }*/
    //测试简单对象
   /*     @RabbitHandler
        @RabbitListener(queues = MQBusinessConstants.BUS_QUEUE)
        @Override
        public void receiveMessage(Message message, MessageConverter messageConverter) {
            Order order = (Order) messageConverter.fromMessage(message);
            System.out.println("消费者收到消息："+order.toString());

        }*/

    //测试字符串(基本数据类型包含String所有的基本类型都可使用Object进行接收)
    @RabbitHandler
    @RabbitListener(queues = MQBusinessConstants.BUS_QUEUE)
    @Override
    public void receiveMessage(Message message, MessageConverter messageConverter) {
        byte str = (byte) messageConverter.fromMessage(message);
        System.out.println("消费者收到消息：" + str);
    }

    // List<String>
 /*   @RabbitHandler
    @RabbitListener(queues = MQBusinessConstants.BUS_QUEUE)
    @Override
    public void receiveMessage(Message message, MessageConverter messageConverter) {
        List<String> list = (List<String>) messageConverter.fromMessage(message);
        System.out.println("消费者收到消息：" + list.toString());
        for (String string : list) {
            System.out.println("list<String>:" + string);
        }
    }*/

    // Map<String, List<UserInfo>>//不需要设置映射
 /*   @RabbitHandler
    @RabbitListener(queues = MQBusinessConstants.BUS_QUEUE)
    @Override
    public void receiveMessage(Message message, MessageConverter messageConverter) {
        Map<String, List<Order>> maps = (Map<String, List<Order>>) messageConverter.fromMessage(message);
        String jsonstr=new String(message.getBody());
        for (Map.Entry<String, List<Order>> entry : maps.entrySet()) {
            for (Order order:entry.getValue()){
                System.out.println("消费者收到消息：" + order.toString());
            }
        }
    }*/
}
